package com.bw.module_live.fans.viewmodel

import androidx.lifecycle.viewModelScope
import com.bw.lib_common.base.BaseViewModel
import com.bw.module_live.fans.intent.LiveFansIntent
import com.bw.module_live.fans.model.repository.LiveRepository
import com.bw.module_live.fans.state.AddHotLiveRoomState
import com.bw.module_live.fans.state.EnterRoomState
import com.bw.module_live.fans.state.ExitRoomState
import com.bw.module_live.fans.state.GoodsListState
import com.bw.module_live.fans.state.JoinRedPacketState
import com.bw.module_live.fans.state.SendGiftState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * @author ytx
 * @date 2025-10-27 15:09
 * @description
 */
@HiltViewModel
class LiveViewModel @Inject constructor(val repository: LiveRepository):BaseViewModel<LiveFansIntent>() {
    private val enterRoomState = MutableStateFlow<EnterRoomState>(EnterRoomState.Init)
    val enterRoomState_ = enterRoomState.asStateFlow()

    private val exitRoomState = MutableStateFlow<ExitRoomState>(ExitRoomState.Init)
    val exitRoomState_ = exitRoomState.asStateFlow()


    private val joinRedPacketState = MutableStateFlow<JoinRedPacketState>(JoinRedPacketState.Init)
    val joinRedPacketState_ = joinRedPacketState.asStateFlow()

    private val goodsListState = MutableStateFlow<GoodsListState>(GoodsListState.Init)
    val goodsListState_ = goodsListState.asStateFlow()
    private val addHotLiveRoomState = MutableStateFlow<AddHotLiveRoomState>(AddHotLiveRoomState.Init)
    val addHotLiveRoomState_ = addHotLiveRoomState.asStateFlow()

    private val sendGiftState = MutableStateFlow<SendGiftState>(SendGiftState.Init)
    val sendGiftState_ = sendGiftState.asStateFlow()


    override fun handleIntent() {
        viewModelScope.launch {
            intents.consumeAsFlow().collect{
                when(it){
                    is LiveFansIntent.EnterRoom -> {
                        enterRoom(it)
                    }
                    is LiveFansIntent.ExitRoom -> {
                        exitRoom(it)
                    }
                    is LiveFansIntent.JoinRedPacket -> {
                        joinRedPacket( it)
                    }
                    is LiveFansIntent.GoodsList -> {
                        goodsList(it)
                    }
                    is LiveFansIntent.AddHotLiveRoom -> {
                        addHotLiveRoom(it)
                    }
                    is LiveFansIntent.SendGift -> {
                        sendGift(it)
                    }
                }
            }
        }

    }

    private fun sendGift(it: LiveFansIntent.SendGift) {
        viewModelScope.launch {
            repository.sendRoomGift(it.entity)
                .catch {
                    sendGiftState.value = SendGiftState.Error
                }.filter {
                    it != null
                }.collect{
                    if (it.code == 200){
                        if (it.data){
                            sendGiftState.value = SendGiftState.Success
                        }
                    }else{
                        sendGiftState.value = SendGiftState.Error
                    }
                }
        }
    }

    /**
     * 点赞直播间
     */

    private fun addHotLiveRoom(it: LiveFansIntent.AddHotLiveRoom) {
        viewModelScope.launch {
            repository.addHotLiveRoom(it.roomId)
                .catch {
                    addHotLiveRoomState.value = AddHotLiveRoomState.Error
                }.filter {
                    it != null
                }.collect{
                    if (it.code == 200){
                        if (it.data){
                            addHotLiveRoomState.value = AddHotLiveRoomState.Success
                        }else{
                            addHotLiveRoomState.value = AddHotLiveRoomState.Error
                        }
                    }else{
                        addHotLiveRoomState.value = AddHotLiveRoomState.Error
                    }
                }
        }
    }

    /**
     * 查看当前直播间商品信息
     */

    private fun goodsList(it: LiveFansIntent.GoodsList) {
        viewModelScope.launch {

            repository.goodsList(it.roomId)
                .onStart {
                    goodsListState.value = GoodsListState.Loading
                }
                .catch {
                    goodsListState.value = GoodsListState.Error
                }.filter {
                    it != null
                }.collect{
                    if (it.code == 200){
                        if (it.data.records.isNullOrEmpty()){
                            goodsListState.value = GoodsListState.Empty
                        }else{
                            goodsListState.value = GoodsListState.Success(it.data)
                        }
                    }else{
                        goodsListState.value = GoodsListState.Error
                    }
                }
        }

    }

    /**
     * 参与抽奖
     */

    private fun joinRedPacket(intent: LiveFansIntent.JoinRedPacket) {
        viewModelScope.launch {
            repository.joinRedPacket(intent.roomId)
                .catch {
                    joinRedPacketState.value = JoinRedPacketState.Error
                }.filter {
                    it != null
                }.collect{
                    if (it.code == 200){
                        if (it.data){
                            joinRedPacketState.value = JoinRedPacketState.Success
                        }
                    }else{
                        joinRedPacketState.value = JoinRedPacketState.Error
                    }
                }
        }
    }

    /**
     * 加入直播间
     */
    private fun enterRoom(intent: LiveFansIntent.EnterRoom) {
        viewModelScope.launch {
            repository.enterLiveRoom(intent.roomId)
                .catch {
                    enterRoomState.value = EnterRoomState.Error
                }.filter {
                    it != null
                }.collect{
                    if (it.code == 200){
                        if (it.data){
                            enterRoomState.value = EnterRoomState.Success
                        }
                    }else{
                        enterRoomState.value = EnterRoomState.Error
                    }
                }


        }

    }

    /**
     * 退出直播间
     */
    private fun exitRoom(intent: LiveFansIntent.ExitRoom) {
        viewModelScope.launch {
            repository.exitLiveRoom(intent.roomId)
                .catch {
                    exitRoomState.value = ExitRoomState.Error
                }.filter {
                    it != null
                }.collect{
                    if (it.code == 200){
                        if (it.data){
                            exitRoomState.value = ExitRoomState.Success
                        }
                    }else{
                        exitRoomState.value = ExitRoomState.Error
                    }
                }


        }
    }
}